package ru.ncedu.java.dmsi.robots.strategies.impl;

import ru.ncedu.java.dmsi.robots.actions.Action;
import ru.ncedu.java.dmsi.robots.actions.impl.MoveAction;
import ru.ncedu.java.dmsi.robots.model.*;
import ru.ncedu.java.dmsi.robots.strategies.RobotStrategy;

public class BorderGuardMoveStrategy implements RobotStrategy {
    public Action getAction(ActionContext context) {
        PointFactory pf = PointFactory.getInstance();
        Point oldP = context.getCurrentLocation();
        // We use that world is rectangle
        // Go to the border
        if (oldP.getX() == context.getMinXCoord() && oldP.getY() < context.getMaxYCoord())
            return new MoveAction(pf.getPoint(oldP.getX(), oldP.getY() + 1));
        if (oldP.getY() == context.getMaxYCoord() && oldP.getX() < context.getMaxXCoord())
            return new MoveAction(pf.getPoint(oldP.getX() + 1, oldP.getY()));
        if (oldP.getX() == context.getMaxXCoord() && oldP.getY() > context.getMinYCoord())
            return new MoveAction(pf.getPoint(oldP.getX(), oldP.getY() - 1));
        if (oldP.getY() == context.getMinYCoord() && oldP.getX() > context.getMinXCoord())
            return new MoveAction(pf.getPoint(oldP.getX() - 1, oldP.getY()));
        return new MoveAction(pf.getPoint(oldP.getX() - 1, oldP.getY()));
    }
}
